package quartz.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.util.StopWatch;

/**
 * JOB 耗时监听
 */
@Slf4j
public class JobConsumingTimeListener implements JobExecutionListener {
    private final StopWatch stopWatch = new StopWatch();

    @Override
    public void beforeJob(JobExecution jobExecution) {
        stopWatch.start();
        log.info("Job before " + jobExecution.getJobParameters());
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        stopWatch.stop();
        log.info("Job Status: {}", jobExecution.getStatus());
        if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
            log.info("Job Finished");
        } else if (jobExecution.getStatus() == BatchStatus.FAILED) {
            log.info("Job Failed");
        }
        log.info("Job Cost Time: {} s", stopWatch.getTotalTimeSeconds());
    }
}